/**
 * @description 删除作品
 */

const { updateWork } = require('../../service/works')
const { ErrorRes, SuccessRes } = require('../../res-model/index')
const { deleteWorkFailInfo, deleteWorkDbErrorFailInfo } = require('../../res-model/failInfo/index')

/**
 * 删除作品
 * @param {string} id workId
 * @param {string} author 作者 username
 * @param {boolean} putBack 恢复删除，默认 false
 */
async function deleteWork(id, author, putBack = false) {
  try {
    // 假删除，更新 status
    const status = putBack === true ? 1 : 0
    const res = await updateWork(
      { status },
      { id, author } // 条件里加 author ，防止删除别人的项目
    )
    if (res) {
      return new SuccessRes()
    }
    return new ErrorRes(deleteWorkFailInfo, 'id 或 author 不匹配')
  } catch (ex) {
    console.error('删除作品错误', ex)
    return new ErrorRes(deleteWorkDbErrorFailInfo)
  }
}

/**
 * 恢复删除
 * @param {string} id id
 * @param {string} author 作者 username
 */
async function putBackWork(id, author) {
  const res = await deleteWork(id, author, true)
  return res
}

module.exports = {
  deleteWork,
  putBackWork
}
